/****************************/
/** COMPUTE EX-ANTE SEARCH **/
/****************************/


void compute_search(){

double testval1, testval2, testval3, testval4, iverif;

struct searchEorWstruct paramsS1D;

for(int sgrid = 0.0; sgrid < maxsearch; sgrid++){

    diffvalues[sgrid] = expspace(sgrid,gridminsearch,gridmaxsearch,EchelleW,maxsearch); // take diffvalue on the possible grid of distance between VF //

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // SEARCHING FOR A BUSINESS IDEA (given a specified s_w <- reduce 3D search to 2D search in VFI) //
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    
    paramsS1D.Psw = max(0.000000000001,diffvalues[sgrid]);
    
    testval1 = searchEwithWgivenfun(0.00001, &paramsS1D);
    testval2 = searchEwithWgivenfun(0.000011, &paramsS1D);
    testval3 = searchEwithWgivenfun(EffortMaxE, &paramsS1D);
    testval4 = searchEwithWgivenfun(EffortMaxE-0.00001, &paramsS1D);

    iverif = 0;
    if(testval1 > testval2 & testval1 < 0.0) {search2D_E[sgrid] = 0.0;iverif = 100;}
    if(testval1 < testval2 & testval1 > 0.0) {search2D_E[sgrid]  = 0.0;iverif = 100;}
    if(testval3 > testval4 & testval3 < 0.0) {search2D_E[sgrid]  = EffortMaxE;iverif = 100;}
    if(testval3 < testval4 & testval3 > 0.0) {search2D_E[sgrid]  = EffortMaxE;iverif = 100;}
    if(iverif == 0) {search2D_E[sgrid] = zbrentNEW(searchEwithWgivenfun,0.0,EffortMaxE,(1.0e-16),&paramsS1D);}



    ///////////////////////////////////////////////////////////////////////////////////////////////////
    // SEARCHING FOR A BUSINESS IDEA (given a specified s_w <- reduce 3D search to 2D search in VFI) //
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    
    paramsS1D.diffValueNextfoc = max(0.000000000001, diffvalues[sgrid]);
    
    testval1 = searchEfun(0.00001, &paramsS1D);
    testval2 = searchEfun(0.000011, &paramsS1D);
    testval3 = searchEfun(EffortMaxE, &paramsS1D);
    testval4 = searchEfun(EffortMaxE-0.00001, &paramsS1D);

    iverif = 0;
    if(testval1 > testval2 & testval1 < 0.0) {search1D_E[sgrid] = 0.0;iverif = 100;}
    if(testval1 < testval2 & testval1 > 0.0) {search1D_E[sgrid]  = 0.0;iverif = 100;}
    if(testval3 > testval4 & testval3 < 0.0) {search1D_E[sgrid]  = EffortMaxE;iverif = 100;}
    if(testval3 < testval4 & testval3 > 0.0) {search1D_E[sgrid]  = EffortMaxE;iverif = 100;}
    if(iverif == 0) {search1D_E[sgrid] = zbrentNEW(searchEfun,0.0,EffortMaxE,(1.0e-16),&paramsS1D);}



    /////////////////////////////////////////////////////////////////////////////////////////////////
    // SEARCHING FOR A JOB (when searching for a business idea at the same time - Unemployed case) //
    /////////////////////////////////////////////////////////////////////////////////////////////////

    for(int tgrid = 0; tgrid < maxtheta; tgrid++) {
        paramsS1D.tgridparXX = tgrid;
        
        testval1 = searchWfun(0.0, &paramsS1D);
        testval2 = searchWfun(0.0001, &paramsS1D);
        testval3 = searchWfun(EffortMaxW, &paramsS1D);
        testval4 = searchWfun(EffortMaxW-0.0001, &paramsS1D);
        
        iverif = 0;
        if(testval1 > testval2 & testval1 < 0.0) {search1D_W[inxS(sgrid, tgrid)] = 0.0;iverif = 100;}
        if(testval1 < testval2 & testval1 > 0.0) {search1D_W[inxS(sgrid, tgrid)] = 0.0;iverif = 100;}
        if(testval3 > testval4 & testval3 < 0.0) {search1D_W[inxS(sgrid, tgrid)] = EffortMaxW;iverif = 100;}
        if(testval3 < testval4 & testval3 > 0.0) {search1D_W[inxS(sgrid, tgrid)]  = EffortMaxW;iverif = 100;}
        if(iverif == 0) {search1D_W[inxS(sgrid, tgrid)] = zbrentNEW(searchWfun,0.0,EffortMaxW,(1.0e-16),&paramsS1D);}
      
    } // end tgrid

} // end sgrid


/* IF SE AND SW ARE FIXED FOR UNEMPLOYED WORKERS */
#if holdfixedSE == 1
    readinputDOUBLE(fixedSEUiE, ifulldim, "OUTPUT/bench/fileSEUiE.out");
    readinputDOUBLE(fixedSEUiNE, ifulldim, "OUTPUT/bench/fileSEUiNE.out");
#endif

#if holdfixedSW == 1
    readinputDOUBLE(fixedSWUiE, ifulldim, "OUTPUT/bench/fileSWUiE.out");
    readinputDOUBLE(fixedSWUiNE, ifulldim, "OUTPUT/bench/fileSWUiNE.out");
#endif


}
